#include "DockWidgetTitleBar.h"
#include "Asset/EngineDefaultPackage.h"

static SColor HighlightColor=  SColor::Green;

RDockWidgetTitleBar::RDockWidgetTitleBar()
{
    this->bClipSubWidgets=true;

    Background =NewWidget<RRectangleWidget>(this);

    HighlightBar =NewWidget<RRectangleWidget>(Background);
    CloseButton=NewWidget<RImageWidget>(Background);
    CloseButtonMouse=NewWidget<RMouseWidget>(CloseButton);
    Text =NewWidget<RTextWidget>(Background);

    Background->Anchor.Fill(this);
    Background->FillColor=SColor::FromHexRGB(0x2d2d2d);

    HighlightBar->Height=2.0f;
    HighlightBar->Anchor.TopReference=Background;
    HighlightBar->Anchor.TopOffset=2.0f;

    HighlightBar->Anchor.LeftReference=Background;
    HighlightBar->Anchor.LeftOffset=2.0f;
    HighlightBar->Anchor.RightReference=Background;
    HighlightBar->Anchor.RightOffset=2.0f;
    HighlightBar->FillColor=HighlightColor;

    CloseButton->Width=15;
    CloseButton->Anchor.RightReference=Background;
    CloseButton->Anchor.RightOffset=2.0f;

    CloseButton->Height=15;
    CloseButton->Anchor.VerticalCenterReference=Background;
    CloseButton->Image= REngineDefaultPackage::Get()->GetCloseIcon();

    CloseButtonMouse->Anchor.Fill(CloseButton);
    CloseButtonMouse->OnClicked.BindLambda([this](const SMouseButtonEvent& )->bool
    {
        OnCloseButtonClickedEvent.ExecuteIfBound(this);
        return true;
    });

    Text->Anchor.VerticalFill(Background);
    Text->Anchor.LeftReference=Background;
    Text->Anchor.LeftOffset=2.0f;
    Text->Anchor.RightReference=CloseButton;
    Text->Anchor.RightReferenceEdge= EHorizontalWidgetEdge::Left;
    Text->Anchor.RightOffset=2.0f;

    Text->HorizontalAlignment= ETextHorizontalAlignment::Center;
    Text->VerticalAlignment= ETextVerticalAlignment::Center;
    
}

void RDockWidgetTitleBar::SetTitle(const CString &InTitle)
{
    Text->Text=InTitle;
}

CString RDockWidgetTitleBar::GetTitle() const
{
    return Text->Text.Get();
}

void RDockWidgetTitleBar::Highlight()
{
    HighlightBar->FillColor=HighlightColor;
}

void RDockWidgetTitleBar::Unhighlight()
{
    HighlightBar->FillColor=SColor::FromHexRGB(0X444444);
}

bool RDockWidgetTitleBar::IsHighlighted() const
{
    return HighlightBar->FillColor.Get()==HighlightColor;
}

TDelegate<void, RDockWidgetTitleBarBasePtr> &RDockWidgetTitleBar::OnCloseButtonClicked()
{
    return OnCloseButtonClickedEvent;
}
